package com.teamclub.erp.sservices

import io.ebean.{Ebean, ExpressionList}
import com.teamclub.domain.erp.dealer.DealerInfoLink
import com.teamclub.domain.system.{AppDomainInstance, AppFuncTree, AppRole, AppSubjectUser}
import com.teamclub.erp.sforms.DealerInfoLinkForm
import com.teamclub.util.errors.ErrorCode
import com.teamclub.util.libs.{F, MD5}
import com.teamclub.util.page.Pager
import org.apache.commons.lang3.StringUtils
import org.slf4j.LoggerFactory
import org.springframework.beans.BeanUtils
import org.springframework.stereotype.Service

import scala.collection.JavaConversions._

/**
  * Created by ilkkzm on 17-9-17.
  */

@Service
class DealerInfoLinkService {
  val logger = LoggerFactory.getLogger(classOf[DealerInfoLinkService])
  def expr(where: ExpressionList[DealerInfoLink],form: DealerInfoLinkForm) = {
    if(form.id != null) {
      where.eq("id", form.id)
    }
    if(StringUtils.isNotBlank(form.dealerOrganNo)) {
      where.eq("dealerOrganNo", form.dealerOrganNo)
    }
    if(StringUtils.isNotBlank(form.producerOrganNo)) {
      where.eq("producerOrganNo", form.producerOrganNo)
    }
  }

  def judgeIsDealer(form: DealerInfoLinkForm): F.Either[Boolean, ErrorCode] = {
    val instance = Ebean.find(classOf[AppDomainInstance]).where().eq("organNo", form.organNo).findOne();
    if(instance == null) {
      return F.Either.Right(ErrorCode.BIZ_FAILURE)
    }
    if("WINDER-DEALER" == instance.getAppDomain) {
      return F.Either.Left(true)
    } else {
      return F.Either.Left(false)
    }
  }

  def list(form: DealerInfoLinkForm): F.Either[Pager[DealerInfoLink], ErrorCode] = {
    val where = Ebean.find(classOf[DealerInfoLink]).where()
    expr(where, form)
    F.Either.Left(Pager.genePager(where.query(), form))
  }

  def geneAdminRole(organNo: String, funcTrees: String): AppRole = {
    val role = new AppRole
    role.setName("管理员")
    role.setOrganNo(organNo)
    role.setRoleKey("admin")
    role.setFuncTreeRef(funcTrees)
    role.save()
    role
  }

  def geneAppSubjectUser(organNo: String, appRole: Integer): AppSubjectUser = {
    val user = new AppSubjectUser
    user.setUserName(organNo)
    user.setRealName("管理员")
    user.setUserPassword(MD5.md5Hex("admin123"))
    user.setOrganNo(organNo)
    user.setRoleId(appRole)
    user.setIsAdmin(true)
    user.save()
    user
  }

  def addBase(form: DealerInfoLinkForm): String = {
    val funcTreeList = Ebean.find(classOf[AppFuncTree]).where().eq("appDomain", form.appDomain).findList()
    val keys = funcTreeList.map(_.getId)
    val instance = new AppDomainInstance
    instance.setAppDomain(form.appDomain)
    instance.setName(form.name)
    instance.setFuncTreeRef(keys.mkString(","))
    instance.setIsAdmin(false)
    instance.save()
    val organNo = String.valueOf(24101000 + instance.getId)
    instance.setOrganNo(organNo)
    instance.update()
    val role = geneAdminRole(organNo, keys.mkString(","))
    geneAppSubjectUser(organNo, role.getId)
    organNo
  }


  def add(form: DealerInfoLinkForm): F.Either[DealerInfoLink, ErrorCode] = {
    var instance = new DealerInfoLink
    if(form.id != null) {
      instance = Ebean.find(classOf[DealerInfoLink], form.id)
    }
    if(instance == null) {
      F.Either.Right(ErrorCode.ENTITY_NOT_FOUND)
    }
    BeanUtils.copyProperties(form, instance)
    if(form.id == null) {
      instance.save()
    } else {
      instance.update()
    }
    F.Either.Left(instance)
  }

  def del(form: DealerInfoLinkForm): F.Either[DealerInfoLink, ErrorCode] = {
    val instance  = Ebean.find(classOf[DealerInfoLink], form.id)
    if(instance == null) {
      F.Either.Right(ErrorCode.ENTITY_NOT_FOUND)
    } else {
      instance.delete()
    }
//    val trans = Ebean.beginTransaction()
//    try {
//      Ebean.find(classOf[AppDomainInstance]).where().eq("organNo", form.dealerOrganNo).delete()
//      Ebean.find(classOf[AppRole]).where().eq("organNo", form.dealerOrganNo).delete()
//      Ebean.find(classOf[AppSubjectUser]).where().eq("organNo", form.dealerOrganNo).delete()
//      dealerInfo.delete()
//      trans.commit()
//    } catch {
//      case e: Exception => {
//        logger.error("", e)
//        trans.rollback()
//      }
//    } finally {
//      trans.end()
//    }
    F.Either.Left(instance)
  }

  def menusync = ???

}
